# !/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText

from loguru import logger


def get_html(sn, stock_number, stock_name, days, datas):
    header = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
    title = f"{stock_number}-{stock_name}-在近{days}交易日成交额{len(datas) - 1}次创新低"
    rows = datas[0].split(' ')
    th = f"<body text='#000000'><center><a href='https://xueqiu.com/S/{stock_number}'>{title}</a></center>" \
         f"<br/><table bgcolor='#B0E0E6' style=' font-size: 14px;'border='1' cellspacing='0' cellpadding='0' bordercolor='#000000' width='95%' align='center' >" \
         "<tr  bgcolor='#F79646' align='left' >" \
         f"<th style='width:200px'>{rows[0]}</th>" \
         f"<th style='width:200px'>{rows[1]}</th>" \
         f"<th style='width:200px'>{rows[2]}</th>" \
         f"<th style='width:200px'>{rows[3]}</th>" \
         "</tr>"
    tr = ""
    for index, row in enumerate(datas[1:]):
        rows = row.split(' ')
        td = ''
        # if str(row[3]).startswith('-'):
        td = td + "<td style='color:green'>" + str(rows[0]) + "</td>"
        td = td + "<td style='color:green'>" + str(rows[1]) + "</td>"
        td = td + "<td style='color:green'>" + str(rows[2]) + "</td>"
        td = td + "<td style='color:green'>" + str(rows[3]) + "</td>"
        # else:
        # td = td + "<td style='color:red'>" + str(rows[0]) + "</td>"
        # td = td + "<td style='color:red'>" + str(rows[1]) + "</td>"
        # td = td + "<td style='color:red'>" + str(rows[2]) + "</td>"
        # td = td + "<td style='color:red'>" + str(rows[3]) + "</td>"
        tr = tr + "<tr>" + td + "</tr>"

    body = str(tr)
    tail = f'</table></body></html>'
    html_txt = header + th + body + tail
    return html_txt


def send_html(industry, stock_number, stock_name, days, datas):
    # 邮箱服务器地址
    # 发件人邮箱地址
    # 发件人邮箱密码(使用申请的客户端授权码代替)
    smtp_server = 'smtp.qq.com'
    from_addr = '83852084@qq.com'
    passwd = 'ruqbqiqjmnjhbgih'

    # 收件人邮箱地址
    to_addr = 'stockbing@icloud.com'

    # 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)
    # msg = MIMEText(str(data), 'plain', 'utf-8')
    html = get_html(industry, stock_number, stock_name, days, datas)
    msg = MIMEText(html, _subtype='html', _charset='utf-8')

    # 设置邮件主题
    msg['Subject'] = Header(f'{industry}-{stock_name}-近{days}个交易日成交额{len(datas) - 1}次创新低', charset='utf-8')
    msg['from'] = from_addr
    msg['to'] = to_addr

    # 获取SMTP对象
    # server = smtplib.SMTP(host=smtp_server, port=25)
    server = smtplib.SMTP_SSL(host=smtp_server, port=465)
    # 登录163邮箱服务器
    server.login(user=from_addr, password=passwd)
    # 发送邮件
    server.sendmail(from_addr=from_addr, to_addrs=to_addr, msg=msg.as_string())
    # 退出邮箱服务器
    server.quit()
    logger.info('邮件发送成功')


if __name__ == '__main__':
    industry = '服装'
    stock_number = 'SZ001234'
    stock_name = '泰慕士'
    days = 30
    datas = ['名称 代码 时间 成交额', '泰慕士 001234.SZ 2023-03-23 13785.0', '泰慕士 001234.SZ 2023-03-24 14307.0',
             '泰慕士 001234.SZ 2023-04-03 14693.0', '泰慕士 001234.SZ 2023-04-06 12713.0',
             '泰慕士 001234.SZ 2023-04-07 12997.0', '泰慕士 001234.SZ 2023-04-10 13622.0',
             '泰慕士 001234.SZ 2023-04-11 8104.0',
             '泰慕士 001234.SZ 2023-04-12 12202.0', '泰慕士 001234.SZ 2023-04-14 13372.0',
             '泰慕士 001234.SZ 2023-04-17 8462.0']
    send_html(industry, stock_number, stock_name, days, datas)
